package com.example.carrierapp.ui.main.waybill.details

import android.support.v7.widget.GridLayoutManager
import com.example.carrierapp.base.DataBindingActivity
import com.example.carrierapp.config.toast
import com.example.carrierapp.ui.adapter.EmptyViewAdapter
import com.example.carrierapp.util.DisplayUtil
import com.example.carrierapp.util.GridSpacingItemDecoration
import com.example.carrierapp.viewmodel.viewmodel.WaybillDetailsModel
import com.example.lb.fingerdemo.R
import kotlinx.android.synthetic.main.activity_waybill_detail_yes.*

class WaybillDetailsActivity : DataBindingActivity<WaybillDetailsModel, WaybillDetailsContract.View, WaybillDetailsPresenter>(), WaybillDetailsContract.View {

    private val state by lazy { intent.getIntExtra("state", 1) }

    override fun createPresenter() = WaybillDetailsPresenter(this)

    override fun getContentView() = R.layout.activity_waybill_details
    override fun getTitleString() = ""
    override fun showBottomLine() = false
    override fun initData() {
        mPresenter.getWaybillDetail(intent.getIntExtra("id", 0))
    }

    override fun getWaybillDetailSuccess(bean: WaybillDetailsModel?) {
        bean?.state = state
        bean?.let { notifyModel(it) }
        initRecycleView(bean?.loadingPicPath, bean?.unloadingPicPath)
    }

    override fun getWaybillDetailFailde(msg: String?) {
        toast(msg)
    }

    private fun initRecycleView(loadingPicPath: MutableList<String>?, unloadingPicPath: MutableList<String>?) {
        unloadingPicPath?.let { loadingPicPath?.addAll(it) }
        loadingPicPath?.let {
            val gridLayoutManager = GridLayoutManager(this, 3)
            recycler_view.layoutManager = gridLayoutManager
            recycler_view.addItemDecoration(GridSpacingItemDecoration(3, DisplayUtil.dip2px(this, 10f), true))
            recycler_view.setHasFixedSize(true)
            recycler_view.setLoadingMoreEnabled(false)
            recycler_view.setPullRefreshEnabled(false)
            recycler_view.refreshComplete()
            recycler_view.loadMoreComplete()

            val mAdapter = EmptyViewAdapter(it, this)
            recycler_view.adapter = mAdapter
        }
    }
}